﻿using NModBusDrive;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TestModBus.Model;
using WinformControls.Forms;

namespace TestModBus
{
    public partial class Form2 : FrmWithTitle, ILog, IDisposable
    {
        Form2Model form2Model = new Form2Model();
        public Form2()
        {
            InitializeComponent();
            Console.SetOut(new TextBoxWriter(tboxConsole));
            form2Model.bussiness.Logger = this;
            form2Model.bussiness.serialPortUtil.CallDataReceived += SerialPortUtil_CallDataReceived;
            this.FormClosing += Form2_FormClosing;
        }

        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            form2Model.bussiness.serialPortUtil.ClosePort();
        }

        public bool IsStop { get; set; }

        private void SerialPortUtil_CallDataReceived(byte[] bits)
        {
            string str = PublicMethod.ByteToHexStr(bits);
            if (str.IndexOf("FFFF") == 0) // 机械矩阵
            {
                if (bits[3] == 1)    // 动作完成
                {
                    Console.WriteLine( "动作完成");
                    SetSingleSingeColor(form2Model.bussiness.operTypeBt, ColorType.Success);
                    IsStop = true;
                    SetSingleSingeColor(form2Model.bussiness.operTypeBt, ColorType.Success);
                    SetCheckEnable(true);
                }
                else if (bits[3] == 2) // X轴故障报警
                {
                    Console.WriteLine("X轴故障报警");
                    SetSingleSingeColor(form2Model.bussiness.operTypeBt, ColorType.Error);
                    SetCheckEnable(true);
                    IsStop = true;
                }
                else if (bits[3] == 3) // Y轴故障报警
                {
                    Console.WriteLine("Y轴故障报警");
                    SetSingleSingeColor(form2Model.bussiness.operTypeBt, ColorType.Error);
                    SetCheckEnable(true);
                    IsStop = true;
                }
                else if (bits[3] == 4) // Z轴故障报警
                {
                    Console.WriteLine( "Z轴故障报警");
                    SetSingleSingeColor(form2Model.bussiness.operTypeBt, ColorType.Error);
                    SetCheckEnable(true);
                    IsStop = true;
                }
                else if (bits[3] == 5) // 设备处于手动状态
                {
                    Console.WriteLine("设备处于手动状态");
                    SetSingleSingeColor(form2Model.bussiness.operTypeBt, ColorType.Error);
                    SetCheckEnable(true);
                    IsStop = true;
                }
                else if (bits[3] == 6) // 设备处于急停状态
                {
                    Console.WriteLine("设备处于急停状态");
                    SetSingleSingeColor(form2Model.bussiness.operTypeBt, ColorType.Error);
                    SetCheckEnable(true);
                    IsStop = true;
                }
                else if (bits[3] == 7) // 指令冲突
                {
                    Console.WriteLine("指令冲突");
                    SetSingleSingeColor(form2Model.bussiness.operTypeBt, ColorType.Error);
                    SetCheckEnable(true);
                    IsStop = true;
                }
                else if (bits[3] == 8) // 运行中切换
                {
                    Console.WriteLine("运行中切换");
                    SetSingleSingeColor(form2Model.bussiness.operTypeBt, ColorType.Error);
                    SetCheckEnable(true);
                    IsStop = true;
                }
            }
            else //FFEE 射频矩阵
            {
                if (bits[3] == 1)    // 动作完成
                {
                    form2Model.tmer.Enabled = false;
                    Console.WriteLine("动作完成");
                    SetCheckEnable(true);
                    IsStop = true;
                }
                else if (bits[3] == 2) // 动作失败
                {
                    form2Model.tmer.Enabled = false;
                    Console.WriteLine("动作失败");
                    SetCheckEnable(true);
                    IsStop = true;
                }
            }
        }

        /// <summary>
        /// 通道切换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ChBt_Click(object sender, EventArgs e)
        {
            try
            {
                SetCheckEnable(false);
                SetCHDefaultState();
                Button button = (Button)sender;
                switch ((ModeOperType)Convert.ToInt32(button.Tag))
                {
                    case ModeOperType.CH1:
                        form2Model.bussiness.operTypeBt = ModeOperType.CH1;
                        form2Model.SetRunCh("CH1");
                        break;
                    case ModeOperType.CH2:
                        form2Model.bussiness.operTypeBt = ModeOperType.CH2;
                        form2Model.SetRunCh("CH2");
                        break;
                    case ModeOperType.CH3:
                        form2Model.bussiness.operTypeBt = ModeOperType.CH3;
                        form2Model.SetRunCh("CH3");
                        break;
                    case ModeOperType.CH4: 
                        form2Model.bussiness.operTypeBt = ModeOperType.CH4;
                        form2Model.SetRunCh("CH4");
                        break;
                    case ModeOperType.CH5:
                        form2Model.bussiness.operTypeBt = ModeOperType.CH5;
                        form2Model.SetRunCh("CH5");
                        break;
                    case ModeOperType.CH6:
                        form2Model.bussiness.operTypeBt = ModeOperType.CH6;
                        form2Model.SetRunCh("CH6");
                        break;
                    case ModeOperType.CH7:
                        form2Model.bussiness.operTypeBt = ModeOperType.CH7;
                        form2Model.SetRunCh("CH7");
                        break;
                    case ModeOperType.CH8:
                        form2Model.bussiness.operTypeBt = ModeOperType.CH8;
                        form2Model.SetRunCh("CH8");
                        break;
                    case ModeOperType.CH9:
                        form2Model.bussiness.operTypeBt = ModeOperType.CH9;
                        form2Model.SetRunCh("CH9");
                        break;
                    case ModeOperType.CH10:
                        form2Model.bussiness.operTypeBt = ModeOperType.CH10;
                        form2Model.SetRunCh("CH10");
                        break;
                    case ModeOperType.CH11:
                        form2Model.bussiness.operTypeBt = ModeOperType.CH11;
                        form2Model.SetRunCh("CH11");
                        break;
                    case ModeOperType.CH12:
                        form2Model.bussiness.operTypeBt = ModeOperType.CH12;
                        form2Model.SetRunCh("CH12");
                        break;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format("{0}\r\n{1}", ex.Message, ex.StackTrace));
            }
        }

        private void SetChBtEnable(bool isEnabed)
        {
            this.Invoke(new MethodInvoker(() =>
            {
                this.Ch1Bt.Enabled = isEnabed;
                this.Ch2Bt.Enabled = isEnabed;
                this.Ch3Bt.Enabled = isEnabed;
                this.Ch4Bt.Enabled = isEnabed;
                this.Ch5Bt.Enabled = isEnabed;
                this.Ch6Bt.Enabled = isEnabed;
                this.Ch7Bt.Enabled = isEnabed;
                this.Ch8Bt.Enabled = isEnabed;
                this.Ch9Bt.Enabled = isEnabed;
                this.Ch10Bt.Enabled = isEnabed;
                this.Ch11Bt.Enabled = isEnabed;
                this.Ch12Bt.Enabled = isEnabed;
            }));
        }

        private void SetSingleSingeColor(ModeOperType modeOper, ColorType color)
        {
            this.Invoke(new MethodInvoker(() =>
            {
                switch (modeOper)
                {
                    case ModeOperType.CH1:
                        this.Ch1.BackColor = form2Model.SetRunColorState(color);
                        break;
                    case ModeOperType.CH2:
                        this.Ch2.BackColor = form2Model.SetRunColorState(color);
                        break;
                    case ModeOperType.CH3:
                        this.Ch3.BackColor = form2Model.SetRunColorState(color);
                        break;
                    case ModeOperType.CH4:
                        this.Ch4.BackColor = form2Model.SetRunColorState(color);
                        break;
                    case ModeOperType.CH5:
                        this.Ch5.BackColor = form2Model.SetRunColorState(color);
                        break;
                    case ModeOperType.CH6:
                        this.Ch6.BackColor = form2Model.SetRunColorState(color);
                        break;
                    case ModeOperType.CH7:
                        this.Ch7.BackColor = form2Model.SetRunColorState(color);
                        break;
                    case ModeOperType.CH8:
                        this.Ch8.BackColor = form2Model.SetRunColorState(color);
                        break;
                    case ModeOperType.CH9:
                        this.Ch9.BackColor = form2Model.SetRunColorState(color);
                        break;
                    case ModeOperType.CH10:
                        this.Ch10.BackColor = form2Model.SetRunColorState(color);
                        break;
                    case ModeOperType.CH11:
                        this.Ch11.BackColor = form2Model.SetRunColorState(color);
                        break;
                    case ModeOperType.CH12:
                        this.Ch12.BackColor = form2Model.SetRunColorState(color);
                        break;
                }
            }));
        }

        /// <summary>
        /// 设置通道默认颜色
        /// </summary>
        public void SetCHDefaultState()
        {
            Ch1.BackColor = Color.DarkGray;
            Ch2.BackColor = Color.DarkGray;
            Ch3.BackColor = Color.DarkGray;
            Ch4.BackColor = Color.DarkGray;
            Ch5.BackColor = Color.DarkGray;
            Ch6.BackColor = Color.DarkGray;
            Ch7.BackColor = Color.DarkGray;
            Ch8.BackColor = Color.DarkGray;
            Ch9.BackColor = Color.DarkGray;
            Ch10.BackColor = Color.DarkGray;
            Ch11.BackColor = Color.DarkGray;
            Ch12.BackColor = Color.DarkGray;
        }

        private void BtClear_Click(object sender, EventArgs e)
        {
            this.tboxConsole.Clear();
        }

        /// <summary>
        /// 信号源-高频/低频-中频输入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void K1Bt_Checked(object sender, EventArgs e)
        {
            SetCheckEnable(false);
            CheckBox check = (CheckBox)sender;
            if (check.Checked)  //选中设置参数
            {
                form2Model.SetShePinArgs(ModeOperType.Y7);
                form2Model.SetShePinArgs(ModeOperType.Y2);
            }
            else   // 未选中取消设置参数
            {
                form2Model.SetShePinArgs(ModeOperType.Y7);
                form2Model.SetShePinArgs(ModeOperType.Y2);
            }
        }

        /// <summary>
        /// 信号源-合成-中频输入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ChkK2_CheckedChanged(object sender, EventArgs e)
        {
            SetCheckEnable(false);
            CheckBox check = (CheckBox)sender;
            if (check.Checked)
            {
                form2Model.SetShePinArgs(ModeOperType.Y10);
                form2Model.SetShePinArgs(ModeOperType.Y2);
            }
            else
            {
                form2Model.SetShePinArgs(ModeOperType.Y10);
                form2Model.SetShePinArgs(ModeOperType.Y2);
            }
        }

        /// <summary>
        /// 信号源-I/Q-中频输入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ChkK3_CheckedChanged(object sender, EventArgs e)
        {
            SetCheckEnable(false);
            CheckBox check = (CheckBox)sender;
            if (check.Checked)
            {
                form2Model.SetShePinArgs(ModeOperType.Y11);
                form2Model.SetShePinArgs(ModeOperType.Y2);
            }
            else
            {
                form2Model.SetShePinArgs(ModeOperType.Y11);
                form2Model.SetShePinArgs(ModeOperType.Y2);
            }
        }

        /// <summary>
        /// 信号源-备用-中频输入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ChkK4_CheckedChanged(object sender, EventArgs e)
        {
            SetCheckEnable(false);
            CheckBox check = (CheckBox)sender;
            if (check.Checked)
            {
                form2Model.SetShePinArgs(ModeOperType.Y12);
                form2Model.SetShePinArgs(ModeOperType.Y2);
            }
            else
            {
                form2Model.SetShePinArgs(ModeOperType.Y12);
                form2Model.SetShePinArgs(ModeOperType.Y2);
            }
        }

        /// <summary>
        /// 信号源-合成-天线
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ChkK5_CheckedChanged(object sender, EventArgs e)
        {
            SetCheckEnable(false);
            CheckBox check = (CheckBox)sender;
            if (check.Checked)
            {
                form2Model.SetShePinArgs(ModeOperType.Y10);
                //form2Model.SetShePinArgs(ModeOperType.Y2);
                form2Model.SetShePinArgs(ModeOperType.Y15);
            }
            else
            {
                form2Model.SetShePinArgs(ModeOperType.Y10);
                //form2Model.SetShePinArgs(ModeOperType.Y2);
                form2Model.SetShePinArgs(ModeOperType.Y15);
            }
        }

        /// <summary>
        /// 信号源-I/Q-天线
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ChkK6_CheckedChanged(object sender, EventArgs e)
        {
            SetCheckEnable(false);
            CheckBox check = (CheckBox)sender;
            if (check.Checked)
            {
                form2Model.SetShePinArgs(ModeOperType.Y11);
                //form2Model.SetShePinArgs(ModeOperType.Y2);
                form2Model.SetShePinArgs(ModeOperType.Y15);
            }
            else
            {
                form2Model.SetShePinArgs(ModeOperType.Y11);
                //form2Model.SetShePinArgs(ModeOperType.Y2);
                form2Model.SetShePinArgs(ModeOperType.Y15);
            }
        }

        /// <summary>
        /// 信号源-高频/低频-天线
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ChkK7_CheckedChanged(object sender, EventArgs e)
        {
            SetCheckEnable(false);
            CheckBox check = (CheckBox)sender;
            if (check.Checked)
            {
                form2Model.SetShePinArgs(ModeOperType.Y7);
                //form2Model.SetShePinArgs(ModeOperType.Y2);
                form2Model.SetShePinArgs(ModeOperType.Y15);
            }
            else
            {
                form2Model.SetShePinArgs(ModeOperType.Y7);
                //form2Model.SetShePinArgs(ModeOperType.Y2);
                form2Model.SetShePinArgs(ModeOperType.Y15);
            }
        }

        /// <summary>
        /// 信号源-备用-天线
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ChkK8_CheckedChanged(object sender, EventArgs e)
        {
            SetCheckEnable(false);
            CheckBox check = (CheckBox)sender;
            if (check.Checked)
            {
                form2Model.SetShePinArgs(ModeOperType.Y12);
                //form2Model.SetShePinArgs(ModeOperType.Y2);
                form2Model.SetShePinArgs(ModeOperType.Y15);
            }
            else
            {
                form2Model.SetShePinArgs(ModeOperType.Y12);
                //form2Model.SetShePinArgs(ModeOperType.Y2);
                form2Model.SetShePinArgs(ModeOperType.Y15);
            }
        }

        /// <summary>
        /// 中频输出-频谱仪
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ChkK9_CheckedChanged(object sender, EventArgs e)
        {
            SetCheckEnable(false);
            CheckBox check = (CheckBox)sender;
            if (check.Checked)
            {
                //form2Model.SetShePinArgs(ModeOperType.Y0);
                form2Model.SetShePinArgs(ModeOperType.Y1);
            }
            else
            {
                //form2Model.SetShePinArgs(ModeOperType.Y0);
                form2Model.SetShePinArgs(ModeOperType.Y1);
            }
        }

        /// <summary>
        /// 中频输出-噪声仪
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ChkK10_CheckedChanged(object sender, EventArgs e)
        {
            SetCheckEnable(false);
            CheckBox check = (CheckBox)sender;
            if (check.Checked)
            {
                form2Model.SetShePinArgs(ModeOperType.Y0);
            }
            else
            {
                form2Model.SetShePinArgs(ModeOperType.Y0);
            }
        }

        /// <summary>
        /// 噪声仪-天线
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ChkK11_CheckedChanged(object sender, EventArgs e)
        {
            SetCheckEnable(false);
            CheckBox check = (CheckBox)sender;
            if (check.Checked)
            {
                form2Model.SetShePinArgs(ModeOperType.Y16);
            }
            else
            {
                form2Model.SetShePinArgs(ModeOperType.Y16);
            }
        }

        /// <summary>
        /// 中频输出-噪声仪-天线
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ChkK12_CheckedChanged(object sender, EventArgs e)
        {
            SetCheckEnable(false);
            CheckBox check = (CheckBox)sender;
            if (check.Checked)
            {
                form2Model.SetShePinArgs(ModeOperType.Y0);
                form2Model.SetShePinArgs(ModeOperType.Y16);
            }
            else
            {
                form2Model.SetShePinArgs(ModeOperType.Y0);
                form2Model.SetShePinArgs(ModeOperType.Y16);
            }
        }

        /// <summary>
        /// 天线-频谱仪
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ChkK13_CheckedChanged(object sender, EventArgs e)
        {
            SetCheckEnable(false);
            CheckBox check = (CheckBox)sender;
            if (check.Checked)
            {
                form2Model.SetShePinArgs(ModeOperType.Y20);
                form2Model.SetShePinArgs(ModeOperType.Y3);
                //form2Model.SetShePinArgs(ModeOperType.Y1);
            }
            else
            {
                form2Model.SetShePinArgs(ModeOperType.Y20);
                form2Model.SetShePinArgs(ModeOperType.Y3);
                //form2Model.SetShePinArgs(ModeOperType.Y1);
            }
        }

        /// <summary>
        /// 天线-矢网
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ChkK14_CheckedChanged(object sender, EventArgs e)
        {
            SetCheckEnable(false);
            CheckBox check = (CheckBox)sender;
            if (check.Checked)
            {
                form2Model.SetShePinArgs(ModeOperType.Y17);
            }
            else
            {
                form2Model.SetShePinArgs(ModeOperType.Y17);
            }
        }

        /// <summary>
        /// 天线-功率计
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ChkK15_CheckedChanged(object sender, EventArgs e)
        {
            SetCheckEnable(false);
            CheckBox check = (CheckBox)sender;
            if (check.Checked)
            {
                form2Model.SetShePinArgs(ModeOperType.Y20);
                form2Model.SetShePinArgs(ModeOperType.Y4);
            }
            else
            {
                form2Model.SetShePinArgs(ModeOperType.Y20);
                form2Model.SetShePinArgs(ModeOperType.Y4);
            }
        }

        /// <summary>
        /// 天线-频率计
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ChkK16_CheckedChanged(object sender, EventArgs e)
        {
            SetCheckEnable(false);
            CheckBox check = (CheckBox)sender;
            if (check.Checked)
            {
                form2Model.SetShePinArgs(ModeOperType.Y20);
                form2Model.SetShePinArgs(ModeOperType.Y5);
            }
            else
            {
                form2Model.SetShePinArgs(ModeOperType.Y20);
                form2Model.SetShePinArgs(ModeOperType.Y5);
            }
        }

        /// <summary>
        /// 天线-示波器-3
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ChkK17_CheckedChanged(object sender, EventArgs e)
        {
            SetCheckEnable(false);
            CheckBox check = (CheckBox)sender;
            if (check.Checked)
            {
                form2Model.SetShePinArgs(ModeOperType.Y20);
                form2Model.SetShePinArgs(ModeOperType.Y6);
            }
            else
            {
                form2Model.SetShePinArgs(ModeOperType.Y20);
                form2Model.SetShePinArgs(ModeOperType.Y6);
            }
        }

        private void BtRun_Click(object sender, EventArgs e)
        {
            form2Model.RunMaxShePinChange();
        }

        /// <summary>
        /// 设置CheckBox是否可用
        /// </summary>
        private void SetCheckEnable(bool isEnble)
        {
            SetChBtEnable(isEnble);
            this.Invoke(new MethodInvoker(() =>
            {
                this.ChkK1.Enabled = isEnble;
                this.ChkK2.Enabled = isEnble;
                this.ChkK3.Enabled = isEnble;
                this.ChkK4.Enabled = isEnble;
                this.ChkK5.Enabled = isEnble;
                this.ChkK6.Enabled = isEnble;
                this.ChkK7.Enabled = isEnble;
                this.ChkK8.Enabled = isEnble;
                this.ChkK9.Enabled = isEnble;
                //this.ChkK10.Enabled = isEnble;
                //this.ChkK11.Enabled = isEnble;
                this.ChkK12.Enabled = isEnble;
                this.ChkK13.Enabled = isEnble;
                this.ChkK14.Enabled = isEnble;
                this.ChkK15.Enabled = isEnble;
                this.ChkK16.Enabled = isEnble;
                this.ChkK17.Enabled = isEnble;
            }));
        }

        #region 日志
        public void Debug(string content)
        {
            Console.WriteLine(content);
        }

        public void Error(string content)
        {
            Console.WriteLine(content);
        }

        public void Error(Exception ex)
        {
            Console.WriteLine(string.Format("{0}\r\n{1}", ex.Message, ex.StackTrace));
        }

        public void Info(string content)
        {
            Console.WriteLine(content);
        }

        public void Write(string log)
        {
            Console.WriteLine(log);
        }

        #endregion

        private void Button_Paint(object sender, PaintEventArgs e)
        {
            Button bt = sender as Button;
            GraphicsPath mygren = new GraphicsPath();
            System.Drawing.Rectangle rang = bt.ClientRectangle;
            rang.Inflate(-2, -1);
            e.Graphics.DrawEllipse(System.Drawing.Pens.BlanchedAlmond, rang);
            rang.Inflate(-2, -4);
            mygren.AddEllipse(rang);
            bt.Region = new Region(mygren);
        }
    }
}